package com.dg11185.nearshop.activity.shop;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.app.ConvertUnit;
import com.dg11185.nearshop.app.MyApplication;
import com.dg11185.nearshop.app.ViewUtils;
import com.dg11185.nearshop.net.entity.Comment;
import com.dg11185.nearshop.net.entity.CommentReply;
import com.dg11185.nearshop.net.request.GeneralRequest;
import com.dg11185.nearshop.net.request.GetReplyCommentListRequest;
import com.dg11185.nearshop.net.response.GetReplyCommentListResponse;
import com.dg11185.nearshop.net.response.NetStatus;
import com.dg11185.nearshop.view.adapter.ReplyCommentAdapter;
import com.wlper.lib.view.loading.LoadLayoutWithTweenAnim;

/**
 * 回复评论界面
 * 
 * @author：Daibo
 * @time：2014-6-4 上午9:15:22
 */
public class ReplyCommentActivity extends SubPageActivity implements OnClickListener {

    private EditText replyContent;

    private ListView replyList;

    private ReplyCommentAdapter listAdapter;

    private List<CommentReply> listDatas = new ArrayList<CommentReply>();

    private Comment comment = null;

    private CommentReply replyObject = null;

    private int curPageNo = 1, total = 0, pageSize = 20;

    private GetReplyCommentListRequest request = null;

    private GeneralRequest replyRequest = null;

    private LoadLayoutWithTweenAnim loadingView = null;

    private Dialog submitDialog = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_comment_reply);
        setSubPageTitle("评论回复");

        submitDialog = ViewUtils.createSubmittingDialog(this);
        findViewById(R.id.reply_main_layout).setOnClickListener(this);
        loadingView = (LoadLayoutWithTweenAnim) this.findViewById(R.id.reply_loading_layout);
        replyContent = (EditText) this.findViewById(R.id.replay_eidttext);
        findViewById(R.id.replay_submit).setOnClickListener(this);
        initTitleComment();
        replyList = (ListView) this.findViewById(R.id.reply_list);
        replyList.setOnItemClickListener(new ItemClickListener());
        listAdapter = new ReplyCommentAdapter(this, listDatas);
        replyList.setAdapter(listAdapter);
        replyRequest = new GeneralRequest(GeneralRequest.REPLY_COMMENT);
        request = new GetReplyCommentListRequest(comment.merchantId, comment.commentId).add("orderName", "createTime")
                .add("orderType", "0");
        initReplyList();
    }

    @Override
    protected void onDestroy() {
        NetClient.httpCancel(mActivity, true);
        super.onDestroy();
    }
    private void initTitleComment(){

        TextView commentContent = (TextView) this.findViewById(R.id.reply_content);
        TextView commentTime = (TextView) this.findViewById(R.id.reply_time);
        TextView replyNum = (TextView) this.findViewById(R.id.reply_number);
        this.findViewById(R.id.reply_more_reply).setOnClickListener(this);
        comment = getIntent().getExtras().getParcelable("mainComment");
        String content = "<font color=#EC0C65>" + comment.userName + " : " + "</font><font color=#000000> "
                + comment.title + " " + comment.content + " </font>";
        String total = "<font color=#999999>评论(</font><font color=#EC0C65>" + comment.totalComments
                + "</font><font color=#999999>)</font>";
        commentContent.setText(Html.fromHtml(content));
        commentTime.setText(ConvertUnit.dateConvert(comment.createTime));
        replyNum.setText(Html.fromHtml(total));
    }
    private void initReplyList() {
        loadingView.onLoading();
        request.getParams().remove("chshSign");
        request.add("pageNumber", curPageNo + "").add("pageSize", pageSize + "").encryption();
        request.setActionListener(new GetReplyCommentListRequest.ActionListener<GetReplyCommentListResponse>() {
            @Override
            public void onSuccess(GetReplyCommentListResponse result) {
                submitDialog.dismiss();
                loadingView.onLoadSuccess();
                if (result.status == NetStatus.SUCCESS) {
                    total = Integer.parseInt(result.total);
                    listDatas.clear();
                    listDatas.addAll(result.commentReplies);
                    listAdapter.notifyDataSetChanged();
                } else {
                    onFailure(-1);
                }
            }

            @Override
            public void onFailure(int resultCode) {
                loadingView.onLoadFailed();
                submitDialog.dismiss();
            }
        });
        NetClient.httpGet(request);

    }

    private class ItemClickListener implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            replyObject = listDatas.get(position);
            replyContent.setText("");
            replyContent.setHint("回复" + replyObject.userName);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.reply_more_reply:
            if (curPageNo * pageSize >= total) {
                ViewUtils.showToast(this, R.string.no_data_anymore);
            } else {
                curPageNo++;
                initReplyList();
            }
            break;
        case R.id.reply_main_layout:
            replyObject = null;
            replyContent.setText("");
            replyContent.setHint("回复评论");
            break;
        case R.id.replay_submit:
            String s = replyContent.getText().toString().trim();
            if (s == null || "".equals(s)) {
                ViewUtils.showToast(this, "评论信息不能为空");
            } else {
                submitReply();
            }
            break;
        default:
            break;
        }
    }

    private void submitReply() {
        submitDialog.show();
        if (replyRequest.getParams() != null) {
            replyRequest.getParams().remove("chshSign");
        }
        replyRequest.add("merchantId", comment.merchantId).add("content", replyContent.getText().toString().trim())
                .add("userId", MyApplication.getUser().userId).add("userName", ConvertUnit.getShortUserName())
                .add("toReplyUserType", "2");

        if (replyObject == null) {
            replyRequest.add("mainCommentId", comment.commentId).add("toReplyUserId", comment.userId)
                    .add("toReplyUserName", comment.userName).encryption();
        } else {
            replyRequest.add("mainCommentId", replyObject.mainCommentId).add("toReplyUserId", replyObject.userId)
                    .add("toReplyUserName", replyObject.userName).encryption();
        }

        replyRequest.setActionListener(new GeneralRequest.ActionListener<NetStatus>() {

            @Override
            public void onSuccess(NetStatus result) {
                submitDialog.dismiss();
                if (result == NetStatus.SUCCESS) {
                    initReplyList();
                } else {
                    onFailure(-1);
                }
            }

            @Override
            public void onFailure(int resultCode) {
                submitDialog.dismiss();
                if (resultCode == NetClient.NET_DISCONNECT_CODE) {
                    ViewUtils.showToast(ReplyCommentActivity.this, R.string.cant_connect_network);
                } else {
                    ViewUtils.showToast(ReplyCommentActivity.this, "评论失败");
                }
            }
        });
        NetClient.httpPost(replyRequest);
        replyContent.setText("");
    }
}
